package com.xyy.saas.payment.pojo.vo;

import com.xyy.saas.payment.dao.model.FbankPayRecords;
import com.xyy.saas.payment.enums.TradeTypeEnum;
import com.xyy.saas.payment.util.ConcurrentSimpleDateFormatUtil;
import com.xyy.saas.payment.util.DateUtil;
import com.xyy.saas.payment.util.XYYStringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.Date;

@Data
@Slf4j
public class FBankPayRecordsVo {
    /**
     * 充值（1）
     * 用户提现（2）
     * 账户转账（26）
     * 退款（38）
     * 结算到虚户（40）
     * 微信支付（205）
     * 支付宝支付（103）
     */
     public static String RECHARGE = "1";
     public static String WITHDRAWAL = "2";
     public static String ACCOUNT_TRANSFER = "26";
     public static String REFUND = "38";
     public static String SETTLEMENT = "40";
     public static String PAY_WX_MINI = "205";
     public static String PAY_WX_PUB = "202";
     public static String PAY_ALI = "103";

    /**
     * 解析富民对账文件行
     * @param row
     * @return
     */
    public static FbankPayRecords parse(String row) {
        if(StringUtils.isBlank(row)) {
            return null;
        }

        String[] rowDatas = row.split("\\|", -1);

        /**
         * 商户号|请求流水号|交易流水号|交易金额|交易状态|交易类型|交易订单创建时间|交易订单完成时间|出账方账号|出账方户名|出账方银行简称|入账方账号|入账方户名|入账方银行简称|入账方证件号|商户保留域|分账信息|分润信息|交易备注|手续费|担保信息|
         */
        if(rowDatas.length != 22) {
            log.error("数据格式不正确, row=" + row);
            return null;
        }

        FbankPayRecords recordsVo = new FbankPayRecords();
        recordsVo.setMerchantNo(rowDatas[0]);
        recordsVo.setClientSerialNo(rowDatas[1]);
        recordsVo.setOrderNo(rowDatas[2]);
        recordsVo.setAmount(rowDatas[3]);
        recordsVo.setTradeStatus(rowDatas[4]);
        recordsVo.setTradeType(rowDatas[5]);

        try {
            recordsVo.setPayTime(ConcurrentSimpleDateFormatUtil.parse(rowDatas[6], DateUtil.DATEFORMATSECOND));
        }
        catch(Exception ex) {
            log.error(ex.getMessage() + ", row=" + row, ex);
        }

        try {
            recordsVo.setSuccessTime(ConcurrentSimpleDateFormatUtil.parse(rowDatas[7], DateUtil.DATEFORMATSECOND));
        }
        catch(Exception ex) {
            if(RECHARGE.equals(recordsVo.getTradeType())) {
                recordsVo.setSuccessTime(recordsVo.getPayTime());
            }

            log.error(ex.getMessage() + ", row=" + row, ex);
        }

        recordsVo.setOutAccountNo(rowDatas[8]);
        recordsVo.setOutAccountName(rowDatas[9]);
        recordsVo.setOutBankCode(rowDatas[10]);
        recordsVo.setInAccountNo(rowDatas[11]);
        recordsVo.setInAccountName(rowDatas[12]);
        recordsVo.setInBankCode(rowDatas[13]);
        recordsVo.setInCardNo(rowDatas[14]);
        recordsVo.setExtra(rowDatas[15]);
        recordsVo.setPropateInfo(rowDatas[16]);
        recordsVo.setFurunInfo(rowDatas[17]);
        recordsVo.setRemarks(rowDatas[18]);
        recordsVo.setFeeAmount(rowDatas[19]);
        recordsVo.setGuarantee(rowDatas[20]);
        return recordsVo;
    }

    /**
     * 解析富民对账文件行
     * @param row
     * @return
     */
    public static FbankPayRecords parse2(String row) {
        if(StringUtils.isBlank(row)) {
            return null;
        }

        String[] rowDatas = row.split("\\|", -1);

        /**
         * 商户号|商户交易号|中台交易号|金额|状态|交易子类型|交易日期|姓名|账户|证件类型|证件号|手机号|交易备注
         */
        if(rowDatas.length != 13) {
            log.error("数据格式不正确, row=" + row);
            return null;
        }

        FbankPayRecords recordsVo = new FbankPayRecords();
        recordsVo.setMerchantNo(rowDatas[0]);
        recordsVo.setClientSerialNo(rowDatas[1]);
        recordsVo.setOrderNo(rowDatas[2]);
        recordsVo.setAmount(rowDatas[3]);
        recordsVo.setTradeStatus(rowDatas[4]);
        recordsVo.setTradeType(rowDatas[5]);

        try {
            recordsVo.setPayTime(ConcurrentSimpleDateFormatUtil.parse(rowDatas[6], DateUtil.DATEFORMATSECOND));
        }
        catch(Exception ex) {
            log.error(ex.getMessage() + ", row=" + row, ex);
        }

        try {
            recordsVo.setSuccessTime(ConcurrentSimpleDateFormatUtil.parse(rowDatas[6], DateUtil.DATEFORMATSECOND));
        }
        catch(Exception ex) {
            log.error(ex.getMessage() + ", row=" + row, ex);
        }

        recordsVo.setOutAccountNo(rowDatas[8]);
        recordsVo.setOutAccountName(rowDatas[7]);
        recordsVo.setRemarks(rowDatas[12]);
        return recordsVo;
    }

    /**
     * 解析直连微信交易数据
     * 交易时间,公众账号ID,商户号,特约商户号,设备号,微信订单号,商户订单号,用户标识,交易类型,交易状态,付款银行,货币种类,
     * 应结订单金额,代金券金额,微信退款单号,商户退款单号,退款金额,充值券退款金额,退款类型,退款状态,商品名称,商户数据包,
     * 手续费,费率,订单金额,申请退款金额,费率备注
     * @param row
     * @return
     */
    public static FbankPayRecords parse3(String row) {
        if(StringUtils.isBlank(row)) {
            return null;
        }

        String[] rowDatas = row.split(",", -1);

        if(row.contains("交易时间") || row.contains("总交易单数") || rowDatas.length < 10) {
            log.error("数据格式不正确, row=" + row);
            return null;
        }

        FbankPayRecords recordsVo = new FbankPayRecords();
        String type = XYYStringUtils.removeStartSymbol(rowDatas[9], "`");
        recordsVo.setMerchantNo(XYYStringUtils.removeStartSymbol(rowDatas[2], "`"));
        recordsVo.setClientSerialNo("SUCCESS".equals(type) ? XYYStringUtils.removeStartSymbol(rowDatas[6], "`") : XYYStringUtils.removeStartSymbol(rowDatas[15], "`"));
        recordsVo.setOrderNo("SUCCESS".equals(type) ? XYYStringUtils.removeStartSymbol(rowDatas[5], "`") : XYYStringUtils.removeStartSymbol(rowDatas[14], "`"));
        recordsVo.setAmount("SUCCESS".equals(type) ? XYYStringUtils.removeStartSymbol(rowDatas[24], "`") : XYYStringUtils.removeStartSymbol(rowDatas[16], "`"));
        recordsVo.setTradeStatus("1");
        recordsVo.setTradeType("SUCCESS".equals(type) ? TradeTypeEnum.WXPAY.getCode() : TradeTypeEnum.REFUND.getCode());

        try {
            recordsVo.setPayTime(ConcurrentSimpleDateFormatUtil.parse(XYYStringUtils.removeStartSymbol(rowDatas[0], "`"), DateUtil.DATEFORMATSECOND));
            recordsVo.setSuccessTime(ConcurrentSimpleDateFormatUtil.parse(XYYStringUtils.removeStartSymbol(rowDatas[0], "`"), DateUtil.DATEFORMATSECOND));
        }
        catch(Exception ex) {
            log.error(ex.getMessage(), ex);
        }

        recordsVo.setFeeAmount(XYYStringUtils.removeStartSymbol(rowDatas[22], "`"));
        return recordsVo;
    }

    /**
     * 解析平安对账文件，对账文件类型为JY（交易）的数据
     * 序号&记账标志(详见右侧，交易文件记账标志说明) &转出交易网会员代码&转出子账户&转出子账户名称&转入交易网会员代码&转入子账户&转入子账户名称&
     * 交易金额&手续费&交易日期&交易时间&银行见证系统流水号&交易网流水号&备注&订单号&
     */
    public static FbankPayRecords parsePinganCheckBillJY(String row) {
        if(StringUtils.isBlank(row)) {
            return null;
        }

        String[] rowDatas = row.split("&", -1);

        FbankPayRecords recordsVo = new FbankPayRecords();
        String tradeType = rowDatas[1];
        if (tradeType.equals("登记挂账")
                || tradeType.equals("见证+收单资金冻结")) {
            recordsVo.setClientSerialNo(rowDatas[15]);
        } else if (tradeType.equals("支付到平台")
                || tradeType.equals("直接支付T0")
                || tradeType.equals("会员间交易退款")
                || tradeType.equals("资方交易T0")) {
            recordsVo.setClientSerialNo(rowDatas[13]);
        } else if (tradeType.equals("见证+收单资金流水")) {
            recordsVo.setClientSerialNo(rowDatas[15]);
        }  else if (tradeType.equals("会员冻结")) {
            recordsVo.setClientSerialNo(rowDatas[13]);
        } else {
            log.info("FBankPayRecordsVo#parsePinganCheckBillJY other tradeType={}", tradeType);
            if (StringUtils.isBlank(rowDatas[15])) {
                recordsVo.setClientSerialNo(rowDatas[13]);
            } else {
                recordsVo.setClientSerialNo(rowDatas[13] + "|" + rowDatas[15] + "|");
            }
        }
        recordsVo.setMerchantNo("");
        recordsVo.setOrderNo(rowDatas[12]);
        recordsVo.setAmount(rowDatas[8]);
        recordsVo.setTradeStatus("成功");
        recordsVo.setTradeType(tradeType);
        recordsVo.setOutAccountName(rowDatas[4]);
        recordsVo.setOutAccountNo(rowDatas[2]);
        recordsVo.setOutBankCode(rowDatas[3]);
        recordsVo.setInAccountName(rowDatas[7]);
        recordsVo.setInAccountNo(rowDatas[5]);
        recordsVo.setInBankCode(rowDatas[6]);
        recordsVo.setRemarks(rowDatas[14]);

        try {
            recordsVo.setPayTime(ConcurrentSimpleDateFormatUtil.parse(rowDatas[10] + rowDatas[11], DateUtil.DATEFORMATSECOND_2));
            recordsVo.setSuccessTime(ConcurrentSimpleDateFormatUtil.parse(rowDatas[10] + rowDatas[11], DateUtil.DATEFORMATSECOND_2));
        }
        catch(Exception ex) {
            log.error(ex.getMessage(), ex);
        }

        recordsVo.setFeeAmount(rowDatas[9]);
        return recordsVo;
    }

    /**
     * 解析平安对账文件，对账文件类型为CZ（充值）的数据
     * 序号&交易网会员代码&子账户&子账户名称&交易金额&手续费&交易日期&交易时间&银行见证系统流水号&交易网流水号&
     * 备注&记账描述（在途充值、见证+收单充值、转账充值、挂账充值、见证+收单充值）&订单号&
     */
    public static FbankPayRecords parsePinganCheckBillCZ(String row) {
        if(StringUtils.isBlank(row)) {
            return null;
        }

        String[] rowDatas = row.split("&", -1);

        FbankPayRecords recordsVo = new FbankPayRecords();
        String tradeType = rowDatas[11];
        if (tradeType.equals("冻结充值")
                || tradeType.equals("见证+收单充值")
                ||tradeType.equals("冻结充值撤销")
                ||tradeType.equals("在途充值")
                ||tradeType.equals("在途充值撤销")) {
            recordsVo.setClientSerialNo(rowDatas[12]);
        } else {
            log.info("FBankPayRecordsVo#parsePinganCheckBillCZ other tradeType={}", tradeType);
            if (StringUtils.isBlank(rowDatas[12])) {
                recordsVo.setClientSerialNo(rowDatas[9]);
            } else {
                recordsVo.setClientSerialNo(rowDatas[9] + "|" + rowDatas[12] + "|");
            }
        }
        recordsVo.setMerchantNo("");
        recordsVo.setOrderNo(rowDatas[8]);
        recordsVo.setAmount(rowDatas[4]);
        recordsVo.setTradeStatus("成功");
        recordsVo.setTradeType(tradeType);
        if (tradeType.equals("冻结充值撤销")) {
            recordsVo.setOutAccountName(rowDatas[3]);
            recordsVo.setOutAccountNo(rowDatas[1]);
            recordsVo.setOutBankCode(rowDatas[2]);
        } else {
            recordsVo.setInAccountName(rowDatas[3]);
            recordsVo.setInAccountNo(rowDatas[1]);
            recordsVo.setInBankCode(rowDatas[2]);
        }

        recordsVo.setRemarks(rowDatas[10]);

        try {
            recordsVo.setPayTime(ConcurrentSimpleDateFormatUtil.parse(rowDatas[6] + rowDatas[7], DateUtil.DATEFORMATSECOND_2));
            recordsVo.setSuccessTime(ConcurrentSimpleDateFormatUtil.parse(rowDatas[6] + rowDatas[7], DateUtil.DATEFORMATSECOND_2));
        }
        catch(Exception ex) {
            log.error(ex.getMessage(), ex);
        }

        recordsVo.setFeeAmount(rowDatas[5]);
        return recordsVo;
    }

    /**
     * 解析平安对账文件，对账文件类型为TX（提现）的数据
     * 序号&交易网会员代码&子账户&子账户名称&交易金额&手续费&交易日期&交易时间&银行见证系统流水号&交易网流水号&
     * 备注&记账类型（提现、见证+收单退款、见证+收单退款撤销）&订单号&
     */
    public static FbankPayRecords parsePinganCheckBillTX(String row) {
        if(StringUtils.isBlank(row)) {
            return null;
        }

        String[] rowDatas = row.split("&", -1);

        FbankPayRecords recordsVo = new FbankPayRecords();
        String tradeType = rowDatas[11];
        if (tradeType.equals("见证+收单退款")) {
            recordsVo.setClientSerialNo(rowDatas[9]);
        } else {
            log.info("FBankPayRecordsVo#parsePinganCheckBillTX other tradeType={}", tradeType);
            if (StringUtils.isBlank(rowDatas[12])) {
                recordsVo.setClientSerialNo(rowDatas[9]);
            } else {
                recordsVo.setClientSerialNo(rowDatas[9] + "|" + rowDatas[12] + "|");
            }
        }
        recordsVo.setMerchantNo("");
        recordsVo.setOrderNo(rowDatas[8]);
        recordsVo.setAmount(rowDatas[4]);
        recordsVo.setTradeStatus("成功");
        recordsVo.setTradeType(tradeType);
        recordsVo.setOutAccountName(rowDatas[3]);
        recordsVo.setOutAccountNo(rowDatas[1]);
        recordsVo.setOutBankCode(rowDatas[2]);
        recordsVo.setInAccountName("");
        recordsVo.setInAccountNo("");
        recordsVo.setInBankCode("");
        recordsVo.setRemarks(rowDatas[10]);

        try {
            recordsVo.setPayTime(ConcurrentSimpleDateFormatUtil.parse(rowDatas[6] + rowDatas[7], DateUtil.DATEFORMATSECOND_2));
            recordsVo.setSuccessTime(ConcurrentSimpleDateFormatUtil.parse(rowDatas[6] + rowDatas[7], DateUtil.DATEFORMATSECOND_2));
        }
        catch(Exception ex) {
            log.error(ex.getMessage(), ex);
        }

        recordsVo.setFeeAmount(rowDatas[5]);
        return recordsVo;
    }

    /**
     * 解析平安对账文件，对账文件类型为FZ（分账）的数据
     * 序号&记账标志(详见右侧，交易文件记账标志说明) &转出交易网会员代码&转出子账户&转出子账户名称&转入交易网会员代码&转入子账户&转入子账户名称&
     * 交易金额&手续费&交易日期&交易时间&银行见证系统流水号&交易网流水号&备注&订单号&
     */
    public static FbankPayRecords parsePinganCheckBillFZ(String row) {
        if(StringUtils.isBlank(row)) {
            return null;
        }

        String[] rowDatas = row.split("&", -1);

        FbankPayRecords recordsVo = new FbankPayRecords();
        String tradeType = rowDatas[1];
        if (tradeType.equals("分账")) {
            recordsVo.setClientSerialNo(rowDatas[15]);
        } else if (tradeType.equals("分账撤销")) {
            recordsVo.setClientSerialNo(rowDatas[15]);  // 分账撤销支持部分退之后，可以修改为逆向分账单号，现在是正向分账单号
        } else {
            log.info("FBankPayRecordsVo#parsePinganCheckBillFZ other tradeType={}", tradeType);
            if (StringUtils.isBlank(rowDatas[15])) {
                recordsVo.setClientSerialNo(rowDatas[13]);
            } else {
                recordsVo.setClientSerialNo(rowDatas[13] + "|" + rowDatas[15] + "|");
            }
        }
        recordsVo.setMerchantNo("");
        recordsVo.setOrderNo(rowDatas[12]);
        recordsVo.setAmount(rowDatas[8]);
        recordsVo.setTradeStatus("成功");
        recordsVo.setTradeType(tradeType);
        recordsVo.setOutAccountName(rowDatas[4]);
        recordsVo.setOutAccountNo(rowDatas[2]);
        recordsVo.setOutBankCode(rowDatas[3]);
        recordsVo.setInAccountName(rowDatas[7]);
        recordsVo.setInAccountNo(rowDatas[5]);
        recordsVo.setInBankCode(rowDatas[6]);
        recordsVo.setRemarks(rowDatas[14]);

        try {
            recordsVo.setPayTime(ConcurrentSimpleDateFormatUtil.parse(rowDatas[10] + rowDatas[11], DateUtil.DATEFORMATSECOND_2));
            recordsVo.setSuccessTime(ConcurrentSimpleDateFormatUtil.parse(rowDatas[10] + rowDatas[11], DateUtil.DATEFORMATSECOND_2));
        }
        catch(Exception ex) {
            log.error(ex.getMessage(), ex);
        }

        recordsVo.setFeeAmount(rowDatas[9]);
        return recordsVo;
    }

    /**
     * 业务线
     */
    private String businessType;
    /**
     * 企业
     */
    private String businessOrderType;
    /**
     * 商户号
     */
    private String merchantNo;
    /**
     * 请求流水号
     */
    private String clientSerialNo;
    /**
     * 交易流水号
     */
    private String orderNo;
    /**
     * 交易金额
     */
    private String amount;
    /**
     * 交易状态
     */
    private String tradeStatus;
    /**
     * 交易类型
     */
    private String tradeType;
    /**
     * 交易订单创建时间
     */
    private Date payTime;
    /**
     * 交易订单完成时间
     */
    private Date successTime;
    /**
     * 出账方账号
     */
    private String outAccountNo;
    /**
     * 出账方户名
     */
    private String outAccountName;
    /**
     * 出账方银行简称
     */
    private String outBankCode;
    /**
     * 入账方账号
     */
    private String inAccountNo;
    /**
     * 入账方户名
     */
    private String inAccountName;
    /**
     * 入账方银行简称
     */
    private String inBankCode;
    /**
     * 入账方证件号
     */
    private String inCardNo;
    /**
     * 商户保留域
     */
    private String extra;
    /**
     * 分账信息
     */
    private String propateInfo;
    /**
     * 分润信息
     */
    private String furunInfo;
    /**
     * 交易备注
     */
    private String remarks;
    /**
     * 手续费
     */
    private String feeAmount;
    /**
     * 担保信息
     */
    private String guarantee;
}
